<?php
/*
Plugin Name: Archives Plus Widget
Plugin URI: http://onebainbridge.org/#
Description: A little help for my friends.
Author: D. Kretz
Version: 0.0.1
Author URI: http://localhost
*/

class Widget_Archives_Plus extends WP_Widget {

	function Widget_Archives_Plus() {
		$widget_ops = array('classname' => 'widget_archives_plus', 'description' => __( 'A monthly archive of your blog&#8217;s posts for a category') );
		$this->WP_Widget('archives_Plus', __('Archives Plus'), $widget_ops);
	}

	function widget( $args, $instance ) {
		extract($args);
        $category = $instance['category'] 
            ? (int) $instance['category'] 
            : 0;
		$c = $instance['count'] ? '1' : '0';
		$d = $instance['dropdown'] ? '1' : '0';
		$title = apply_filters('widget_title', empty($instance['title']) ? __('Archives Plus') : $instance['title']);

		echo $before_widget;
		if ( $title )
			echo $before_title . $title . $after_title;

		if ( $d ) {
?>
		<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo esc_attr(__('Select Month')); ?></option> <?php wp_get_archives(apply_filters('widget_archives_dropdown_args', array('type' => 'monthly', 'format' => 'option', 'show_post_count' => $c))); ?> </select>
<?php
		} else {
?>
		<ul>
		<?php wp_get_archives(
            apply_filters('widget_archives_args', 
                          array('type' => 'monthly', 
                                'category' => '16',
                                'show_post_count' => $c)
            )
        ); ?>
		</ul>
<?php
		}

		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$new_instance = wp_parse_args( (array) $new_instance, array(    'title' => '', 
             'category' => '',
             'count' => 0, 
             'dropdown' => '') );
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['category'] = strip_tags($new_instance['category']);
		$instance['count'] = $new_instance['count'] ? 1 : 0;
		$instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;

		return $instance;
	}

	function form( $instance ) {
		$categories = get_categories(array("hide_empty" => 0,
                                           "orderby" => "name"));
        $catid          = $this->get_field_id("category");
        $catname        = $this->get_field_name("category");
        $catval         = empty($instance["category"]) 
                            ? false 
                            : $instance["category"];

		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'category', 'count' => 0, 'dropdown' => '') );
		$title = strip_tags($instance['title']);
		$category = isset($instance['category'])
            ?  strip_tags($instance['category'])
            : '';
		$count = $instance['count'] ? 'checked="checked"' : '';
		$dropdown = $instance['dropdown'] ? 'checked="checked"' : '';
?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<?php


// put category picker here
		echo "<p>
            <label for='{$catname}'>";
            _e('Category'); 
            echo "</label>
		<select class='widefat' 
            id='{$catname}' 
            name='{$catname}'>\n";

        foreach ( $categories as $cat ) {
            $id = $cat->term_id;
            $name = $cat->name;
            echo "<option value='{$id}'";
            if(intval($id) == intval($catval))
                echo " selected='selected'";
            echo ">{$name}</option>\n";
        }


?> <p><input class="checkbox" type="checkbox" <?php echo $count; ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
			<br />
			<input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" /> <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as a drop down'); ?></label>
		</p>
<?php
	}
}

/**
  * Register widget.
  *
  * Calls 'widgets_init' action after the widget has been registered.
  */

function archives_plus_init() 
{
	register_widget("Widget_Archives_Plus");
}

add_action("init", "archives_plus_init", 1);
?>
